<template>
  <div class="goods-list">

    <!-- <router-link class="goods-item" v-for="item in goodsList" :key="item.id" 
    :to="'/home/goodsinfo/' + item.id" tag="div">
      <img :src="item.img_url" alt="">
      <h1 class="title">{{ item.title }} </h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{ item.sell_price }}</span>
          <span class="old">￥{{ item.market_price }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </router-link> -->

    <!-- 
      在网页中，有两种跳转方式：
      方式1：使用a标签的形式叫做 标签跳转
      方式2：使用window.location.href的形式，叫着编程式导航
    
     -->
    <div class="goods-item" v-for="item in goodsList" :key="item.id" @click="goDetail(item.id)">
      <img :src="item.img_url" alt="">
      <h1 class="title">{{ item.title }} </h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{ item.sell_price }}</span>
          <span class="old">￥{{ item.market_price }}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </div>



    <mt-button type="danger" size="large" @click="getMore">加载更多..</mt-button>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        pageindex: 1, //分页的页数
        goodsList: []
      }
    },
    created(){
      this.getGoodsList()
    },
    methods:{
      newGuid()
      {
        var guid = "";
        for (var i = 1; i <= 32; i++){
          var n = Math.floor(Math.random()*16.0).toString(16);
          guid +=   n;
          if((i==8)||(i==12)||(i==16)||(i==20))
            guid += "-";
        }
        return guid;    
      },
      getGoodsList(){ //获取商品列表
        //请求获取
        /* this.$http.get("api/getgoods/pageindex=" + this.pageindex).then(result=>{
          if(result.body.status === 0){
            //this.goodsList = result.body.message
            this.goodsList = this.goodsList.concat(result.body.message)
          }
        }) */
        //模拟数据 : https://sale.jd.com/act/8yaD0qZuE6VFk.html
        
        var goodObj1 = {
          // id: this.newGuid(),
          id: 'af990c38-8621-324e-bfce-0fd5b81f9b5d',  //注意这里的id写死为了方便测试后面的购物车，不要点击 【加载更多】否则会报错
          title: "Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机",
          add_time: Date.now(),
          zhaiyao:"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机",
          click:20,
          img_url:"http://m.360buyimg.com/babel/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg",
          sell_price:5499,
          market_price:6499,
          stock_quantity:60
        }
        var goodObj2 = {
          // id: this.newGuid(),
          id: '4af1da8c-8a1b-6f5f-c1e3-fdeb352d4aff',
          title: "Apple iPhone 8 64GB 红色特别版 移动联通电信4G手机",
          add_time: Date.now(),
          zhaiyao:"Apple iPhone 8 64GB 红色特别版 移动联通电信4G手机",
          click:10,
          img_url:"http://m.360buyimg.com/babel/jfs/t19342/199/1516671468/71132/8e37293d/5acc524cN424bbaa0.jpg",
          sell_price:4599,
          market_price:5599,
          stock_quantity:30
        }
        var goodObj3 = {
          id: 'a096d00b-dc27-8c0a-7f76-e931533cc87a',
          title: "Apple iPhone 6s Plus (A1699) 32G 玫瑰金色 移动联通电信4G手机",
          add_time: Date.now(),
          zhaiyao:"Apple iPhone 6s Plus (A1699) 32G 玫瑰金色 移动联通电信4G手机",
          click:0,
          img_url:"http://m.360buyimg.com/babel/jfs/t3175/89/2690068639/114728/22c2edee/57e4a35bN230918c0.jpg",
          sell_price:2999,
          market_price:3999,
          stock_quantity:40
        }
        var goodObj4 = {
          id: '89d101d6-e3d5-31ad-bb72-df8bfdcac86a',
          title: "Apple iPhone 8 Plus (A1864) 64GB 深空灰色 移动联通电信4G手机",
          add_time: Date.now(),
          zhaiyao:"Apple iPhone 8 Plus (A1864) 64GB 深空灰色 移动联通电信4G手机",
          click:15,
          img_url:"http://m.360buyimg.com/babel/jfs/t8107/37/1359438185/72159/a6129e26/59b857f8N977f476c.jpg",
          sell_price:5499,
          market_price:6499,
          stock_quantity:35
        }
        var goodObj5 = {
          id: 'bc831ec8-d6c7-0e2e-70b0-5356fb246240',
          title: "Apple iPhone X (A1903) 64GB 深空灰色 移动联通4G手机",
          add_time: Date.now(),
          zhaiyao:"Apple iPhone X (A1903) 64GB 深空灰色 移动联通4G手机",
          click:15,
          img_url:"http://m.360buyimg.com/babel/jfs/t10675/253/1344769770/66891/92d54ca4/59df2e7fN86c99a27.jpg",
          sell_price:6999,
          market_price:7999,
          stock_quantity:25
        }
        this.goodsList.push(goodObj1);
        this.goodsList.push(goodObj2);
        this.goodsList.push(goodObj3);
        this.goodsList.push(goodObj4);
        this.goodsList.push(goodObj5);
        //下面是为了在购物车页面展示购买的商品，这里暂时保存到本地存储中
        localStorage.setItem('GoodsList',JSON.stringify(this.goodsList))
      },
      getMore(){
        this.pageindex++;
        this.getGoodsList();
      },
      goDetail(id){ //使用JS形式进行路由导航
        // 注意：区分 this.$route 和 this.$router这两个对象
        // 其中：this.$route是路由【参数对象】，所有路由中的参数，params,query都属于它
        // 其中：this.$router时一个路由【导航对象】，用它可以方便的使用JS代码，实现路由的前进后退，跳转到新的URL
        // 1.简单使用
        // this.$router.push("/home/goodsinfo/" + id)
        // 2.传递对象
        // this.$router.push({path: "/home/goodsinfo/" + id})
        // 3.传递命名的路由
        this.$router.push({name:"goodsinfo", params: {id} })
        // 返回到上一步
        // this.$router.go(-1)
      }
    }
  }
</script>

<style lang="scss" scoped>
/* 
经典两列布局：
.goods-list（包裹的大容器）
    display:flex;
    flex-wrap: wrap;//换行
    justify-content: space-between; //两端对齐
.goods-item（里面的每一项）
    width:49%;

纵轴布局：
  display: flex;  //让每一项变成纵向flex布局，解决因为有些标题多行导致空白格的出现
  flex-direction: column;  //默认是row橫向
  justify-content: space-between;
*/
.goods-list{
  display:flex;
  flex-wrap: wrap;//换行
  padding: 7px;
  justify-content: space-between; //两端对齐
  .goods-item{
    width:49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding:2px;
    display: flex;  //让每一项变成纵向flex布局，解决因为有些标题多行导致空白格的出现
    flex-direction: column;  //默认是row橫向
    justify-content: space-between;
    min-height: 293px; //解决因为网络延迟而出现高度等的情况
    img{
      width:100%;  //让图片撑满
    }
    .title{
      font-size: 14px;
    }
    .info{
      background-color: #eee;
      p{
        margin: 0;
        padding:5px;
      }
      .price{
        .now{
          color:red;
          font-weight: bold;
          font-size: 16px;
        }
        .old{
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell{
        display: flex;
        justify-content: space-between;
        font-size: 12px;
      }
    }
  

  }


}
</style>
